package com.inei.eea2014.model.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Capitulo1;
import com.inei.eea2014.model.entity.Capitulo2;

public class Capitulo2Dao implements ICRUD<Capitulo2> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public Capitulo2Dao(Context context) {
		util = new UtilDB(context);
	}

	// metodos del CRUD
	@Override
	public Capitulo2 buscar(Capitulo2 o) {
		Capitulo2 obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery(
				"select * from Capitulo2 where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Capitulo2();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setC0002_001_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_001_01_N")));
			obj.setC0002_002_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_002_01_N")));
			obj.setC0002_003_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_003_01_N")));
			obj.setC0002_004_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_004_01_N")));
			obj.setC0002_005_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_005_01_N")));
			obj.setC0002_006_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_006_01_N")));
			obj.setC0002_007_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_007_01_N")));
			obj.setC0002_008_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_008_01_N")));
			obj.setC0002_009_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_009_01_N")));
			obj.setC0002_010_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_010_01_N")));
			obj.setC0002_011_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_011_01_N")));
			obj.setC0002_012_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_012_01_N")));
			obj.setC0002_013_01_N(cursor.getInt(cursor
					.getColumnIndex("C0002_013_01_N")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Capitulo2> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Capitulo2 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("USUARIO", o.getUSUARIO());
		valores.put("C0002_001_01_N", o.getC0002_001_01_N());
		valores.put("C0002_002_01_N", o.getC0002_002_01_N());
		valores.put("C0002_003_01_N", o.getC0002_003_01_N());
		valores.put("C0002_004_01_N", o.getC0002_004_01_N());
		valores.put("C0002_005_01_N", o.getC0002_005_01_N());
		valores.put("C0002_006_01_N", o.getC0002_006_01_N());
		valores.put("C0002_007_01_N", o.getC0002_007_01_N());
		valores.put("C0002_008_01_N", o.getC0002_008_01_N());
		valores.put("C0002_009_01_N", o.getC0002_009_01_N());
		valores.put("C0002_010_01_N", o.getC0002_010_01_N());
		valores.put("C0002_011_01_N", o.getC0002_011_01_N());
		valores.put("C0002_012_01_N", o.getC0002_012_01_N());
		valores.put("C0002_013_01_N", o.getC0002_013_01_N());

		resultado = (int) db.insert("Capitulo2", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Capitulo2 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C0002_001_01_N", o.getC0002_001_01_N());
		valores.put("C0002_002_01_N", o.getC0002_002_01_N());
		valores.put("C0002_003_01_N", o.getC0002_003_01_N());
		valores.put("C0002_004_01_N", o.getC0002_004_01_N());
		valores.put("C0002_005_01_N", o.getC0002_005_01_N());
		valores.put("C0002_006_01_N", o.getC0002_006_01_N());
		valores.put("C0002_007_01_N", o.getC0002_007_01_N());
		valores.put("C0002_008_01_N", o.getC0002_008_01_N());
		valores.put("C0002_009_01_N", o.getC0002_009_01_N());
		valores.put("C0002_010_01_N", o.getC0002_010_01_N());
		valores.put("C0002_011_01_N", o.getC0002_011_01_N());
		valores.put("C0002_012_01_N", o.getC0002_012_01_N());
		valores.put("C0002_013_01_N", o.getC0002_013_01_N());

		resultado = (int) db.update("Capitulo2", valores,
				"RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Capitulo2 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
